package com.censtar.criminalintent;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.censtar.criminalintent.db.CrimeBaseHelper;
import com.censtar.criminalintent.db.CrimeCursorWrapper;
import com.censtar.criminalintent.db.CrimeDbSchema;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import static com.censtar.criminalintent.db.CrimeDbSchema.*;

/**
 * 单例
 */
public class CrimeLab {
    private static CrimeLab sCrimeLab;
    //private List<Crime> mCrimes;
    private Context mContext;
    private SQLiteDatabase mSQLiteDatabase;
    public static CrimeLab get(Context context){
        if(sCrimeLab == null){
            sCrimeLab = new CrimeLab(context);
        }
        return sCrimeLab;
    }
    private static ContentValues getContentValues(Crime crime){
        ContentValues values = new ContentValues();
        values.put(CrimeTable.Cols.UUID,crime.getId().toString());
        values.put(CrimeTable.Cols.TITLE,crime.getTitle());
        values.put(CrimeTable.Cols.DATE,crime.getDate().getTime());
        values.put(CrimeTable.Cols.SOLVED,crime.isSolved()?1:0);
        values.put(CrimeTable.Cols.SUSPECT,crime.getSuspect());

        return values;
    }
    public CrimeLab(Context context) {
        mContext = context.getApplicationContext();
        mSQLiteDatabase = new CrimeBaseHelper(mContext).getWritableDatabase();
    }

    public List<Crime> getCrimes(){
        List<Crime> crimes = new ArrayList<Crime>();
        CrimeCursorWrapper cursorWrapper = queryCrimes(null,null);
        try {
            cursorWrapper.moveToFirst();
            while (!cursorWrapper.isAfterLast()){
                crimes.add(cursorWrapper.getCrime());
                cursorWrapper.moveToNext();
            }
        }catch (Exception e){

        }finally {
            cursorWrapper.close();
        }
        return crimes;
    }
    public Crime getCrime(UUID id){
       CrimeCursorWrapper cursorWrapper = queryCrimes(
               CrimeTable.Cols.UUID + " = ?",
               new String[]{id.toString()}
       );
       try {
           if (cursorWrapper.getCount() == 0){
               return null;
           }
           cursorWrapper.moveToFirst();
           return cursorWrapper.getCrime();
       }finally {
           cursorWrapper.close();
       }
    }
    private CrimeCursorWrapper queryCrimes(String whereClause, String[] whereArgs){
        Cursor cursor = mSQLiteDatabase.query(
                CrimeTable.NAME,
                null,
                whereClause,
                whereArgs,
                null,
                null,
                null
        );
        return new CrimeCursorWrapper(cursor);
    }
    /**
     * 更新
     * @param crime
     */
    public void updateCrime(Crime crime){
        String uuidString = crime.getId().toString();

        ContentValues values = getContentValues(crime);
        mSQLiteDatabase.update(CrimeTable.NAME,values,CrimeTable.Cols.UUID+" = ?",
                new String[]{uuidString});
    }
    public void addCrime(Crime crime){
        ContentValues values = getContentValues(crime);
        mSQLiteDatabase.insert(CrimeTable.NAME,null,values);
    }

    public File getPhotoFile(Crime crime){
        File filesDir = mContext.getFilesDir();//获取/data/data/<包名>/files目录。
        return new File(filesDir,crime.getPhotoFilename());
    }
}
